package com.qiwenshare.file.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.file.api.UserfileHistoryService;
import com.qiwenshare.file.constant.OperationTypeEnum;
import com.qiwenshare.file.vo.UserFileHistoryVo;
import com.qiwenshare.file.vo.user.UserLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 文件操作历史表(UserfileHistory)表控制层
 *
 * @author makejava
 * @since 2023-09-23 20:58:36
 */
@RestController
@RequestMapping("userfileHistory")
public class UserfileHistoryController {
    /**
     * 服务对象
     */
    @Resource
    private UserfileHistoryService userfileHistoryService;

    public static final String CURRENT_MODULE = "文档操作履历管理";

    @Operation(summary = "分页查询操作履历", description = "分页查询操作履历", tags = {"UserFileHistory"})
    @GetMapping(value = "/page")
    @MyLog(operation = "分页查询操作履历", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<UserFileHistoryVo> queryPage(
            @Parameter(description = "用户名", required = false) Integer operationType,
            @Parameter(description = "登录账号", required = false) String createUser,
            @Parameter(description = "文件类型", required = false) Integer fileMode,
            @Parameter(description = "当前页", required = true) long currentPage,
            @Parameter(description = "页面数量", required = true) long pageCount) {
        IPage<UserFileHistoryVo> historyList = userfileHistoryService.getHistoryList(operationType, createUser, fileMode, currentPage, pageCount);
        List<UserFileHistoryVo> historyVos = historyList.getRecords().stream().peek(h -> {
            h.setOperationContent(OperationTypeEnum.getEnum(h.getOperationType(), h.getIsDir()).getDesc());
            h.setOperationType(OperationTypeEnum.getEnum(h.getOperationType(), h.getIsDir()).getType());
            h.setChildren(h.getChildren().stream().peek(hh -> {
                hh.setOperationContent(OperationTypeEnum.getEnum(hh.getOperationType(), hh.getIsDir()).getDesc());
                hh.setOperationType(OperationTypeEnum.getEnum(hh.getOperationType(), hh.getIsDir()).getType());
            }).collect(Collectors.toList()));
        }).collect(Collectors.toList());
        return RestResult.success().dataList(historyVos, historyList.getTotal());
    }

    @Operation(summary = "查询操作履历", description = "查询操作履历", tags = {"UserFileHistory"})
    @GetMapping(value = "/detail")
    @MyLog(operation = "查询操作履历", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<UserFileHistoryVo> queryDetail(
            @Parameter(description = "文件ID", required = true) String userFileId) {
        List<UserFileHistoryVo> historyVos = userfileHistoryService.queryDetail(userFileId).stream().peek(h -> {
            h.setOperationContent(OperationTypeEnum.getEnum(h.getOperationType(), h.getIsDir()).getDesc());
            h.setOperationType(OperationTypeEnum.getEnum(h.getOperationType(), h.getIsDir()).getType());
        }).collect(Collectors.toList());
        return RestResult.success().data(historyVos);
    }
}

